package com.park.web.controller.user;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.dto.ArticleDTO;
import com.park.client.model.article.ArticleCollectModel;
import com.park.client.model.article.ArticleModel;
import com.park.client.model.article.ArticleTypeModel;
import com.park.client.model.pub.SensitiveWordModel;
import com.park.client.service.article.ArticleCollectService;
import com.park.client.service.article.ArticleService;
import com.park.client.service.article.ArticleTypeService;
import com.park.client.service.pub.SensitiveWordService;
import com.park.common.util.Page;
import com.park.common.util.PageData;
import com.park.common.util.QueryParams;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;
import com.park.web.enums.ArticleStatusEnum;
import com.park.web.util.SessionUtil;

@Controller
@RequestMapping("/user/article")
public class UserArticleController extends BaseController{
	@Autowired
	private ArticleService articleService;
	@Autowired
	private ArticleTypeService articleTypeService;
	@Autowired
	private SensitiveWordService sensitiveWordService;
	@Autowired
	private ArticleCollectService collectService;
	
	@RequestMapping("articleManage")
	public String articleManage(){
			return Global.USER_PATH+"article/articleManage";
	}
	
	/**
	 *单个文章详情 
	 */
	@RequestMapping("detail")
	public String detail(@RequestParam("id") Long id,HttpServletRequest request){
		articleService.updateClickRate(id);//每点击一次，加1
		ArticleModel article = articleService.selectByPrimaryKey(id);
		request.setAttribute("article", article);
		return Global.USER_PATH+"article/articleDetail";
	}
	
	/**
	 *根据大的分类查询
	 */
	@RequestMapping("selectByCategory")
	@ResponseBody
	public Result<List<ArticleTypeModel>> selectByCategory(Integer category,HttpServletRequest request){
		Result<List<ArticleTypeModel>> result = new Result<>(true);
		try {
			List<ArticleTypeModel> articleTypes = articleTypeService.selectByCategory(category != null?category:1);
			result.setObj(articleTypes);
		} catch (Exception e) {
			result.setSuccess(false);
		}
		return result;
	}
	
	/**
	 *发布文章
	 */
	@RequestMapping("articleSave")
	@ResponseBody
	public Result<ArticleModel> articleSave(ArticleModel article,HttpServletRequest request,HttpSession session){
			Result<ArticleModel> result = new Result<>(true);
			try {
				article.setCreater(SessionUtil.getUserID(session));
				article.setCreateDate(new Date());
				//过滤敏感词
				String articleContent = article.getArticleContent();
				List<SensitiveWordModel> words = sensitiveWordService.selectAll();
				for (SensitiveWordModel word : words) {
					articleContent = articleContent.replaceAll( word.getWord(), "***");
				}
				article.setArticleContent(articleContent);
				articleService.saveOrUpdate(article);
				result.setMessage("保存成功,等待管理员审核!");
			} catch (Exception e) {
				result.setMessage("保存失败!");
				result.setSuccess(false);
			}
			return result;
	}
	
	/**
	 * 删除到回收站
	 */
	@RequestMapping("putToRecycle")
	public String putToRecycle(Long id){
		articleService.putToRecycle(id);
		return "redirect:articleManage";
	}
	
	/**
	 *设置为草稿
	 */
	@RequestMapping("putToDraft")
	public String putToDraft(Long id){
		 articleService.setToDraft(id);
		return "redirect:articleManage";
	}
	
	/**
	 *草稿页面
	 */
	@RequestMapping("articleDraft")
	public String articleDraft(@RequestParam(name="pageNow",defaultValue="1")Integer pageNow,ArticleModel article,HttpServletRequest request,HttpSession session){
		article.setStatus(ArticleStatusEnum.STATUS_0.code);
		article.setCreater(SessionUtil.getUserID(session));	
		selectByStatus(article, pageNow, request, session);
		return Global.USER_PATH+"article/articleDraft";
	}
	
	@RequestMapping("articleList")
	@ResponseBody
	public PageData<ArticleModel> articleList(QueryParams queryParams,ArticleModel article,HttpSession session){
		PageData<ArticleModel> pageData = new PageData<>();
		article.setCreater(SessionUtil.getUserID(session));	
		Integer total = articleService.selectByStatusTotal(article);
		Page page = new Page(queryParams.getOffset(),total,queryParams.getLimit());
		article.setStartPos(page.getStartPos());
		article.setPageSize(page.getPageSize());
		List<ArticleModel> listArticles = articleService.selectByStatus(article);
		pageData.setTotal(total);
		pageData.setRows(listArticles);
		return pageData;
	}
	
	/**
	 * 回收站
	 */
	@RequestMapping("articleRecycle")
	public String articleRecycle(@RequestParam(name="pageNow",defaultValue="1")Integer pageNow,ArticleModel article,HttpServletRequest request,HttpSession session){
		article.setStatus(ArticleStatusEnum.STATUS_3.code);
		article.setCreater(SessionUtil.getUserID(session));
		selectByStatus(article, pageNow, request, session);
		return Global.USER_PATH+"article/articleRecycle";
	}
	
	private void selectByStatus(ArticleModel article,Integer pageNow,HttpServletRequest request,HttpSession session){
		Integer total = articleService.selectByStatusTotal(article);
		Page page = new Page(pageNow,total,10);
		article.setStartPos(page.getStartPos());
		article.setPageSize(page.getPageSize());
		List<ArticleModel> listArticles = articleService.selectByStatus(article);
		request.setAttribute("page", page);
		request.setAttribute("listArticles", listArticles);
	}
	
	/**
	 * 彻底删除
	 */
	@RequestMapping("deleteArticle")
	@ResponseBody
	public Result<Boolean> deleteArticle(Long id){
		Result<Boolean> result = new Result<>(true);
		boolean success =  articleService.deleteByPrimaryKey(id);
		if(success){
			result.setMessage("删除成功!");
		}else{
			result.setMessage("删除失败!");
			result.setSuccess(false);
		}
		return result;
	}
	
	/**
	 * 从回收站恢复
	 */
	@RequestMapping("recoverFromRecycle")
	@ResponseBody
	public Result<Boolean> recoverFromRecycle(Long id){
		Result<Boolean> result = new Result<>(true);
		boolean success =  articleService.recoverFromRecycle(id);
		if(success){
			result.setMessage("恢复成功!");
		}else{
			result.setMessage("恢复失败!");
			result.setSuccess(false);
		}
		return result;
	}
	
	@RequestMapping("batchDelete")
	@ResponseBody
	public Result<Boolean> batchDelete(@RequestBody ArticleDTO articleDto){
		Result<Boolean> result = new Result<>(true);
		try {
			List<ArticleModel> articleList = articleDto.getArticleList();
			articleService.batchDelete(articleList);
			result.setMessage("删除成功!");
		} catch (Exception e) {
			result.setMessage("删除失败!");
			result.setSuccess(false);
		}
		return result;
	}
	
	@RequestMapping("batchUpdateStatus")
	@ResponseBody
	public Result<Boolean> batchUpdateStatus(@RequestBody ArticleDTO articleDto){
		Result<Boolean> result = new Result<>(true);
		try {
			List<ArticleModel> articleList = articleDto.getArticleList();
			articleService.batchUpdateStatus(articleList);
			result.setMessage("成功放入回收站!");
		} catch (Exception e) {
			result.setMessage("放入回收站失败!");
			result.setSuccess(false);
		}
		return result;
	}
	
	/**
	 *我的收藏
	 */
	@RequestMapping("articleCollect")
	public String articleCollect(HttpServletRequest request,HttpSession session){
		List<ArticleCollectModel> collects =  collectService.list(SessionUtil.getUserID(session));//我的收藏
		request.setAttribute("collects", collects);
		return Global.USER_PATH+"article/articleCollect";
	}
	
}
